package simpleeditor.controllers;

import java.util.LinkedList;
import java.util.List;

import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QSpinBox;
import com.trolltech.qt.gui.QTableWidget;
import com.trolltech.qt.gui.QTableWidgetItem;
import com.trolltech.qt.gui.QWidget;

public abstract class TableEditController extends TableController {
	
	private QSpinBox sb;
	private QLineEdit le;
	private QWidget input;
	private int editedColumn;
	private QComboBox cb;
	
	public TableEditController(QTableWidget table, QPushButton addButton,
			QPushButton removeButton) {
		super(table, addButton, removeButton);
		table.cellDoubleClicked.connect(this,"editItem()");
	}
	
	public abstract void editItem();
	
	private Couple<QTableWidgetItem, Integer> selectedItemData() {
		int row;
		QTableWidgetItem selected;
		
		selected=table.selectedItems().get(0);
		row=selected.row();
		
		return new Couple<QTableWidgetItem,Integer>(selected,row);
	}

	private void drawInputWidget(int row) {
		int rowHeight;
		
		rowHeight=table.rowHeight(row);
		input.setGeometry(table.columnViewportPosition(editedColumn)+table.verticalHeader().width(), table.horizontalHeader().height()+row*rowHeight,table.columnWidth(editedColumn),rowHeight);
		input.setVisible(true);
		input.setFocus();
		
	}

	protected void editInteger(int editedColumn, int minValue, int maxValue){
		Couple<QTableWidgetItem,Integer> data;
		QTableWidgetItem selected;
		
		this.editedColumn=editedColumn;
		
		data=selectedItemData();
		
		selected=data.getFirst();
		
		sb=new QSpinBox(table);
		sb.setMaximum(maxValue);
		sb.setMinimum(minValue);
		sb.setValue(Integer.parseInt(selected.text()));
		sb.editingFinished.connect(this,"integerChanged()");
		input=sb;
		
		drawInputWidget(data.getSecond());
		
	}
	
	protected void editText(int editedColumn){
		Couple<QTableWidgetItem,Integer> data;
		QTableWidgetItem selected;
		
		this.editedColumn=editedColumn;
		
		data=selectedItemData();
		
		selected=data.getFirst();
		
		le=new QLineEdit(selected.text(),table);
		le.editingFinished.connect(this,"textChanged()");
		input=le;
		
		drawInputWidget(data.getSecond());
	}

	@SuppressWarnings("unused")
	private void integerChanged(){
		QTableWidgetItem selected; 
		int row;
		
		if(sb!=null){
			selected= table.selectedItems().get(0);
			row = selected.row();
			
			if(updateModel(row,editedColumn,Integer.parseInt(selected.text()),sb.value())){
				table.setItem(row, editedColumn, new QTableWidgetItem(sb.value()+""));
			}
			sb.setVisible(false);
			sb=null;

		}
	}
	
	@SuppressWarnings("unused")
	private void textChanged(){
		QTableWidgetItem selected;
		int row;
		
		
		if (le!=null){
			selected = table.selectedItems().get(0);
			row = selected.row();
			
			if(updateModel(row,editedColumn,selected.text(),le.text())){
				table.setItem(row, editedColumn, new QTableWidgetItem(le.text()));
			}
			le.setVisible(false);
			le=null;
		}
	}
	
	protected void editChoise(int editedColumn, Object[] values) {
		
		Couple<QTableWidgetItem,Integer> data;
		QTableWidgetItem selected;
		List<String> texts;
		
		this.editedColumn=editedColumn;
		
		data=selectedItemData();
		
		selected=data.getFirst();
		
//		le=new QLineEdit(selected.text(),table);
//		le.editingFinished.connect(this,"textChanged()");
//		input=le;
		
		cb=new QComboBox(table);
//		cb.activatedIndex.connect(this,"choiseEdited(int)");
//		cb.setLineEdit(new QLineEdit());
//		cb.lineEdit().editingFinished.connect(this,"choiseEdited()");
//		cb.activatedIndex.connect(this,"choiseEdited()");
//		cb.activated.connect(this,"choiseEdited()");
//		cb.setLineEdit(new QLineEdit());
//		cb.editTextChanged.connect(this,"choiseEdited()");
		texts=new LinkedList<String>();
		for(int i=0;i<values.length;i++){
			texts.add(values[i].toString());
		}
		cb.addItems(texts);
		cb.setCurrentIndex(texts.indexOf(selected.text()));
		input=cb;
		
		drawInputWidget(data.getSecond());
	}
	
	@SuppressWarnings("unused")
	private void choiseEdited(){
		QTableWidgetItem selected;
		int row;
		
		System.out.println("Trigger");
		
		if (cb!=null){
			selected = table.selectedItems().get(0);
			row = selected.row();
			
			if(updateModel(row,editedColumn, cb.currentIndex(),selected.text(),cb.currentText())){
				table.setItem(row, editedColumn, new QTableWidgetItem(cb.currentText()));
			}
			cb.setVisible(false);
			cb=null;
		}
	}
	
	protected abstract boolean updateModel(int row, int editedColumn2, int currentIndex,
			String text, String currentText);

	protected abstract boolean updateModel(int row,int column,int oldValue,int newValue);
	protected abstract boolean updateModel(int row,int column,String oldValue,String newValue);

}
